/**
 * File    : PasswordRating.java
 * Created : 2008-10-18
 * By      : LuHuiguo
 */
package com.luhuiguo.tapestry.mixins;

import org.apache.tapestry5.Field;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.annotations.MixinAfter;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

/**
 * @author LuHuiguo
 * @version $Id: PasswordRating.java 7 2008-10-18 10:56:07Z LuHuiguo $
 * 
 */
@IncludeJavaScriptLibrary("PasswordRating.js")
@MixinAfter
public class PasswordRating {

	@Inject
	private Messages messages;

	@Environmental
	private RenderSupport renderSupport;

	@InjectContainer
	private Field field;

	@BeginRender
	void beginRender(MarkupWriter writer) {
		writer.attributes("onkeyup", "CreateRatePasswdReq(this)");
	}

	@AfterRender
	void afterRender(MarkupWriter writer) {

		String id = field.getClientId();
		String ratingId = id + ":rating";

		writer.element("table", "id", ratingId, "border", "0", "cellpadding",
				"0", "cellspacing", "0");
		writer.element("tr");
		writer.element("td", "nowrap", "nowrap", "valign", "top", "width", "0");
		writer.element("font", "size", "-1", "face", "Arial, sans-serif");
		writer.write(messages.get("password-strength"));
		writer.end();
		writer.end();
		writer.element("td", "nowrap", "nowrap", "valign", "top");
		writer.element("font", "size", "-1", "face", "Arial, sans-serif",
				"color", "#808080");
		writer.element("strong");
		writer.element("div", "id", "passwdRating");
		writer.end();
		writer.end();
		writer.end();
		writer.end();
		writer.end();

		writer.element("tr");
		writer.element("td", "height", "3");
		writer.end();
		writer.end();

		writer.element("tr");
		writer.element("td", "colspan", "2");
		writer.element("table", "id", "passwdBar", "bgcolor", "#ffffff",
				"border", "0", "cellpadding", "0", "cellspacing", "0", "width",
				"180");
		writer.element("tr");
		writer.element("td", "id", "posBar", "height", "4", "bgcolor",
				"#e0e0e0", "width", "0%");
		writer.end();
		writer.element("td", "id", "negBar", "height", "4", "bgcolor",
				"#e0e0e0", "width", "100%");
		writer.end();
		writer.end();

		writer.end();
		writer.end();
		writer.end();

		writer.end();

		renderSupport.addScript("ratingMsgs = new Array(6);");
		renderSupport.addScript("ratingMsgs[0] = '%s';", messages
				.get("too-short"));
		renderSupport.addScript("ratingMsgs[1] = '%s';", messages.get("weak"));
		renderSupport.addScript("ratingMsgs[2] = '%s';", messages.get("fair"));
		renderSupport.addScript("ratingMsgs[3] = '%s';", messages.get("good"));
		renderSupport
				.addScript("ratingMsgs[4] = '%s';", messages.get("strong"));
		renderSupport.addScript("ratingMsgs[5] = '%s';", messages
				.get("not-rated"));

	}
}
